package com.swxc.smartorder.ui.dialog;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.swxc.smartorder.R;
import com.swxc.smartorder.utils.CountDownTimerUtils;
import com.swxc.smartorder.utils.DisplayUtil;

/**
 * Author   Administrator
 * Date     2018/4/13
 * Desc     eg
 *
 * @author Administrator
 */
public class NoNetworkDialog extends DialogFragment {


    public static boolean isShow = false;
    public static boolean autoDismiss = false;
    private Activity mActivity;
    private boolean isSuccess;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isShow = true;
        mActivity = this.getActivity();
        setStyle(android.support.v4.app.DialogFragment.STYLE_NO_TITLE, R.style.PromptDialog);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mActivity = null;
    }


    @Override
    public void onStart() {
        super.onStart();
        //点击window外的区域 是否消失
        int margin = DisplayUtil.INSTANCE.dip2px(mActivity, 8);
        getDialog().setCanceledOnTouchOutside(true);
        Window window = getDialog().getWindow();
        assert window != null;
        window.setGravity(Gravity.TOP);
        window.getDecorView().setPadding(margin, 8, margin, margin);

        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setDimAmount(0f);
        window.setAttributes(lp);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        if (null != bundle) {

        }
        return inflater.inflate(R.layout.layout_prompt, container, false);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private void initView(View view) {

        if (autoDismiss) {
            CountDownTimerUtils.getCountDownTimer()
                    .setMillisInFuture(1500)
                    .setFinishDelegate(new CountDownTimerUtils.FinishDelegate() {
                        @Override
                        public void onFinish() {
                            dismiss();
                        }
                    }).start();
        }
    }
}
